import java.io.File;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;

public class RidList 
{

	ArrayList<Long> mergedList = new ArrayList<Long>(); 
	datatype[] datatypes = new datatype[7];
	
	public ArrayList<Long> mergedRidList(HashMap<Integer, String> hashMap, String path, ArrayList<datatype> header)
	{
		Set<Integer> set = hashMap.keySet();
		Iterator<Integer> itr= set.iterator();
		index i = null;
		i8 longConverter= new i8();
		datatypes[0] = new i1();
		datatypes[1] = new i2();
		datatypes[2] = new i4();
		datatypes[3] = new i8();
		datatypes[4] = new r4();
		datatypes[5] = new r8();
		datatypes[6] = new cx();
		
		
		for(int k=0; k<hashMap.size();k++)
		{	
			ArrayList<Long> tempList = new ArrayList<Long>();
			int m = path.indexOf(".");
			String fileName = path.substring(0, m);
			int cn=itr.next();
			File f = new File(fileName+"."+cn+".lho");
			try 
			{
				
				IndexHeader ih = new IndexHeader();
				long []temp = ih.readHeader(f);
				i = new index(temp);
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			File f2 = new File(fileName+"."+cn+".lht");
			i.fo=f;
			i.ft=f2;
			/* for debugging
			for(int w =0;w<i.actualSize;w++)
			{
				
				System.out.println("Bucket"+w);
				i.Showbuckets(w, header, cn-1);
			}
			*/
			String str= hashMap.get(cn);
			if(header.get(cn-1).type==6)
			{
				
				int l1=str.length();
				int l2=header.get(cn-1).length;
				if(l1<l2)
				{
					for(int it=0;it<l2-l1;it++)
					{
						str +='~';
					}
				}
			}
			Object o = datatypes[header.get(cn-1).type].convert(str);
			int hashCode = datatypes[header.get(cn-1).type].hashCode(o);
			
			int bucketID=hashCode % (2*(int)i.totalSize);
			
			if(bucketID > i.actualSize-1)
				bucketID -= i.totalSize; 
					
		
			Bucket buck = i.getBucket(bucketID, i.ft);
			ArrayList<String> array;
			ArrayList<String> array2 = new ArrayList<String>();
			while(true)
			{
				array = i.displayBucket(buck,header,cn-1);
				
				for(int j=0;j<array.size();j++)
				{
					array2.add(array.get(j));
				}
				
				if(buck.offset==Long.MAX_VALUE)
					break;
				else
					buck = i.getBucket(buck.offset, i.fo);
			}
		
			for(int q=0; q<array2.size(); q +=2)
			{
				String rid=array2.get(q);
				String data =array2.get(q+1);
			
				if(datatypes[header.get(cn-1).type].convert(str).equals(datatypes[header.get(cn-1).type].convert(data)))
				{
					Long r = (Long)longConverter.convert(rid);
					//compare data with selection and if it matches add rid to an arraylist
					
					if(k==0)
					{
						mergedList.add(r);
					}
					else
						tempList.add(r);
				}

			}
			ArrayList<Long> temp2 = new ArrayList<Long>();
			if(k > 0)
			{
				for(Long t : tempList)
				{
					if(mergedList.contains(t))
						temp2.add(t);
				}
				mergedList = temp2;
			}			
					
			
		}
		return mergedList;
	}
}
